<div class="setting">
    <?php include PROJECT_PATH . 'Application/Web/View/Setting/menu.tpl.php';?>
    <div class="right">
		<div class="class">
			<?php foreach ($class as $key=>$value){?>
			<div class="class_wrapper">
    			<div class="main_class">
    				<input id="sort_<?=$key?>" class="class_sort text" type="text" value="<?=$value['sort']?>" />
    				<input id="content_<?=$key?>" class="class_content text" type="text" value="<?=$value['name']?>" />
    				<span class="btn" onclick="save(<?=$key?>)">保存</span>
    				<span class="btn" onclick="del(<?=$key?>)">删除</span>
    			</div>
    			<?php foreach ($value['subClass'] as $subClass){?>
    			<div class="sub_class">
    				<input id="sort_<?=$subClass['class_id']?>" class="class_sort text" type="text" value="<?=$subClass['class_sort']?>" />
    				<input id="content_<?=$subClass['class_id']?>" class="class_content text" type="text" value="<?=$subClass['class_name']?>" />
    				<span class="btn" onclick="save(<?=$subClass['class_id']?>)">保存</span>
    				<span class="btn" onclick="del(<?=$subClass['class_id']?>)">删除</span>
    			</div>
    			<?php }?>
    			<div class="sub_class">
    				<input id="sort_newsub_<?=$key?>" class="class_sort text" type="text" value="" />
    				<input id="content_newsub_<?=$key?>" class="class_content text" type="text" value="" />
    				<span class="btn" onclick="addSubClass(<?=$key?>)">新增子类</span>
    			</div>
			</div>
			<?php }?>
			<div class="class_wrapper">
    			<div class="main_class">
        			<input id="sort_newmain" class="class_sort text" type="text" value="" />
            	    <input id="content_newmain" class="class_content text" type="text" value="" />
            	    <span class="btn" onclick="addMainClass()">新增父类</span>
    	    	</div>
    	    </div>
		</div>
    </div>
</div>
<script type="text/javascript">
	function save(id)
	{
		var class_id   = id;
		var class_sort = $.trim($("#sort_" + id).val());
		var class_name = $.trim($("#content_" + id).val());
		$.ajax({
			  url: "http://<?=HOST?>/setting/updateClassAjax",
			  cache: false,
			  timeout:10000,
			  type: "POST",
			  data: {
				  class_id:class_id,
				  class_sort:class_sort,
				  class_name:class_name
			  },
			  dataType: "json",
			  beforeSend: function() {
				  disableHTML(true, '请稍等，保存中……', true);
			  },
			  success: function(data) {
				  if(data.errCode != 0){
					  disableHTML(true, data.errMsg, false);
			  		  setTimeout(disableHTML,2000);
					  if(data.errCode == -1){
						  $("#sort_" + id).focus();
					  		
					  }else if(data.errCode == -2){
						  $("#content_" + id).focus(); 
				      }
				  }else{
					  disableHTML(true, '保存成功，跳转中……', true);
					  setTimeout("location.href = 'http://<?=HOST?>/setting/class'",2000);
				  }
			  },
			  error:function(){
	      			disableHTML(true, "请求出错，请重试", false);
	      			setTimeout(disableHTML,2000);
	      		}
			});
	};
	function del(id)
	{
		if(!confirm("确定删除？")) return;
		$.ajax({
			  url: "http://<?=HOST?>/setting/delClassAjax",
			  cache: false,
			  timeout:10000,
			  type: "POST",
			  data: {
				  class_id:id
			  },
			  dataType: "json",
			  beforeSend: function() {
				  disableHTML(true, '请稍等，处理中……', true);
			  },
			  success: function(data) {
				  if(data.errCode == 0){
					  disableHTML(true, '删除成功，跳转中……', true);
					  setTimeout("location.href = 'http://<?=HOST?>/setting/class'",2000);
				  }
				  else
				  {
					  disableHTML(true, data.errMsg, false);
				  	  setTimeout(disableHTML,2000);
				  }
			  },
			  error:function(){
	      			disableHTML(true, "请求出错，请重试", false);
	      			setTimeout(disableHTML,2000);
	      		}
			});
		
	};
	function addSubClass(pid)
	{
		var class_main_id = pid;
		var class_sort = $.trim($("#sort_newsub_" + pid).val());
		var class_name = $.trim($("#content_newsub_" + pid).val());
		$.ajax({
			  url: "http://<?=HOST?>/setting/addSubClassAjax",
			  cache: false,
			  timeout:10000,
			  type: "POST",
			  data: {
				  class_pid:class_main_id,
				  class_sort:class_sort,
				  class_name:class_name
			  },
			  dataType: "json",
			  beforeSend: function() {
				  disableHTML(true, '请稍等，保存中……', true);
			  },
			  success: function(data) {
				  if(data.errCode != 0){
					 disableHTML(true, data.errMsg, false);
				  	 setTimeout(disableHTML,2000);
					 if(data.errCode == -1){
						 $("#sort_newsub_" + pid).focus();
					 }else if(data.errCode == -2){
						 $("#content_newsub_" + pid).focus(); 
					 }
				  }else{
					  disableHTML(true, '保存成功，跳转中……', true);
					  setTimeout("location.href = 'http://<?=HOST?>/setting/class'",2000);
				  }
			  },
			  error:function(){
	      			disableHTML(true, "请求出错，请重试", false);
	      			setTimeout(disableHTML,2000);
	      		}
			});
	};
	function addMainClass()
	{
		var class_sort = $.trim($("#sort_newmain").val());
		var class_name = $.trim($("#content_newmain").val());
		$.ajax({
			  url: "http://<?=HOST?>/setting/addMainClassAjax",
			  cache: false,
			  timeout:10000,
			  type: "POST",
			  data: {
				  class_sort:class_sort,
				  class_name:class_name
			  },
			  dataType: "json",
			  beforeSend: function() {
				  disableHTML(true, '请稍等，保存中……', true);
			  },
			  success: function(data) {
				  if(data.errCode != 0){
					 disableHTML(true, data.errMsg, false);
					 setTimeout(disableHTML,2000);
					 if(data.errCode == -1){
						 $("#sort_newmain").focus();
				  		
					 }else if(data.errCode == -2){
						 $("#content_newmain").focus(); 
					 }
				  }else{
					disableHTML(true, '保存成功，跳转中……', true);
					setTimeout("location.href = 'http://<?=HOST?>/setting/class'",2000);
				  }
			  },
			  error:function(){
	      			disableHTML(true, "请求出错，请重试", false);
	      			setTimeout(disableHTML,2000);
	      		}
			});
	};
</script>